package com.eqsoft.gesturerecognition;

import java.util.Arrays;
import java.util.Random;
import java.util.Vector;

public class VectorQuantizer {
	
	private static final float MIN_CHANGE = 0.01f;
	//private double radius;
	private int numStates, numObservations;
	private float[][] map;
	private boolean mapped;
	
	public VectorQuantizer(int numStates, int numObservations)
	{
		this.numStates = numStates;
		this.numObservations = numObservations;
		map = new float[numObservations][3];
		mapped = false;
	}
	
	public int[][] deriveGroups(Vector<AccelVector> data)
	{
		int[][] groups = new int[map.length][data.size()];
		float[][] d = new float[map.length][data.size()];
		float[] cur = new float[3];
		float[] v = new float[3];
		for (int i = 0; i < map.length; i++)
		{
			float[] ref = map[i];
			for (int j = 0; j < data.size(); j++)
			{
				cur = data.get(j).getVals();
				v[0] = ref[0] - cur[0];
				v[1] = ref[1] - cur[1];
				v[2] = ref[2] - cur[2];
				d[i][j] = (float)Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
			}
		}
		for (int i = 0; i < data.size(); i++)
		{
			float min = Float.MAX_VALUE;
			int row = 0;
			for (int j = 0; j < map.length; j++)
			{
				if (d[i][j] < min)
				{
					min = d[i][j];
					row = i;
				}
				groups[j][i] = 0;
			}
			groups[row][i] = 1;
		}
		
		return groups;
	}
	
	public int[] quantize(Vector<AccelVector> data) {
		int[][] groups = deriveGroups(data);
		for (int i = 0; i < groups.length; i++)
		{
			for (int j = 0; j < groups[0].length; j++)
				System.out.print(groups[i][j] + " ");
			System.out.println();
		}
		
		int[] res = new int[numObservations];
		int i = 0;
		for (int j = 0; j < groups[0].length; j++)
			for (int k = 0; k < groups.length; k++)
				if (groups[k][j] == 1)
				{
					res[i] = k;
					if (i < numObservations - 1) i++;
					break;
				}
		
		return res;
	}
}
